<?php
/**
 * 用户统计
 * User: Acer
 * Date: 2022/7/23
 * Time: 11:45
 */

namespace app\crontab\service;

use app\move\models\IdentityCardModel;
use app\crontab\models\UserCensusModels;
use app\wx\models\UserModel;

class UserShellService {
    // 获取当前总的用户人数
    public function s_user_total($condition = []){
        $userModel = new UserModel();
        if($condition){
            $userModel->where($condition);
        }
        $user_number = $userModel->field("open_id as user_number")->count("open_id");
        return $user_number;
    }

    // 认证人数
    public function s_attestation_user($condition = []){
       $IdentityCardModel =  new IdentityCardModel();
        if($condition){
            $IdentityCardModel->where($condition);
        }
        $attestation_number = $IdentityCardModel->field("did as attestation_number")->count("did");
        return $attestation_number;
    }

    // 更改或者新增数据
    public function s_save_census($data){
        $UserCensusModels = new UserCensusModels();
        $su_id = $UserCensusModels->where('census_time',$data['census_time']." 00:00:00")->value('su_id');
        $install =[
            'total_number'  =>  (int)$data['total_number'], // 总人数
            'add_number'  =>  (int)$data['add_number'], // 新增人数
            'attestation_number'  =>  (int)$data['attestation_number'] , // 认证人数
            'add_attestation_number'  =>  (int)$data['add_attestation_number'], // 新增认证人数
            'census_time'  =>  $data['census_time'], // 统计时间
        ];
        if($su_id){
            // 更新
            $UserCensusModels->save($install,[
                'su_id' => $su_id
            ]);
        }else{
            // 新增
            $UserCensusModels->insertGetId($install);
        }
    }

}